// import Api, {  } from '../../utils/api.js';
const list = [
    {
        id:'12',
        order_sn:"A14548578748",
        pic:"../../../img/public/cart_goods_pic.png",
        goodsName:'商品名商品名称商品名称商品名称',
        price:'15.00',
        ctime:"1508293427",
        status:'0'
    },
    {
        id: '13',
        order_sn: "A14548578748",
        pic: "../../../img/public/cart_goods_pic.png",
        goodsName: '商品名商品名称商品名称商品名称',
        price: '15.00',
        ctime: "1508293427",
        status: '1'
    },
    {
        id: '14',
        order_sn: "A14548578748",
        pic: "../../../img/public/cart_goods_pic.png",
        goodsName: '商品名商品名称商品名称商品名称',
        price: '15.00',
        ctime: "1508293427",
        status: '3'
    }];
const conversions = [
    // {
    //     pic: "../../../img/public/cart_goods_pic.png",
    //     goodsName: '商品名商品名称商品名称商品名称',
    //     price: '15.00',
    //     ctime: "1508293427",
    //     status: '0'
    // }
];
Page({
    data: {
        tab_idx:'0',
        list: [],               //  渲染使用的数据
        
        isLoading: false,       /** 是否在加载中 */
        hasMore: true,          /** 是否有更多顺数据 */
        swith_ing: false,          /** 正在切换吗 */

        lottery_idx: '',        /** 抽奖记录的索引 */
        conver_idx: '',         /** 兑换记录的索引 */
        lottery_id: '',        /** 抽奖记录的ID */
        conver_id: '',         /** 兑换记录的ID */

        dialog: false,          /** 弹窗是否显示 */
        userInfo:{},            /** 用户地址信息 */
        noData: false,          /** 是否有数据 */
    },

    /** 数据初始化 */
    init: function () {
        wx.showLoading({ title: '加载中' });
        
        let noData = false;

        /** 1 => 模拟请求 list */
        // Api.fetch(, obj, resp => { 
            const that = this;
            setTimeout(_ => {
                wx.hideLoading();
                let data = that.process(list);
                data.length == 0 ? noData = true : null;

                that.setData({ list: data, Lottery: data, noData: noData });
            }, 600);
        // })

    },

    /** 数据处理 */
    process:function(data){
        /** 状态
         *  0 信息待填写  ==> 完善信息
         *  1 待发货  ==> 
         *  2 配送中  ==> 查看物流
         *  3 已完成  ==> 再抽一次
         */
        const statusData = ['信息待填写', '待发货', '配送中', '已完成'];

        data.map((item)=>{
            item.Format = this.Format(item.ctime);              // 用于显示的时间
            item.status_txt = statusData[parseInt(item.status)];// 用于显示的状态
            
        });
        console.log(data)
        return data;
    },

    /** 时间戳 */
    Format:function(ctime){
        const date = new Date(parseInt(ctime) * 1000);
        const y = date.getFullYear(), m = date.getMonth() + 1, d = date.getDate();
        const h = date.getHours(), mm = date.getMinutes(), s = date.getSeconds();
        let t = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
        return t;
    },

    /** tab切换*/
    swithc:function(e){
        const that = this;
        const {tip} = e.target.dataset;
        const { tab_idx,Lottery, conversion}=this.data;

        if (tab_idx=='1'&&tip=='l'){
            wx.showLoading({ title: '加载中' });
            this.setData({ 
                tab_idx: 0, 
                list: [], 
                swith_ing:true,
                isLoading:false 
            });

            setTimeout(_=>{
                wx.hideLoading();
                let data = that.process(Lottery);
                let noData = false;
                data.length == 0 ? noData = true : null;

                that.setData({
                    list: Lottery, 
                    swith_ing: false, 
                    noData: noData
                });
            },1000);

       
        } else if (tab_idx == '0' && tip == 'c'){
            wx.showLoading({ title: '加载中' });
            this.setData({ 
                tab_idx: 1, 
                list: [], 
                swith_ing: true,
                isLoading: false  
            });

            setTimeout(_ => {
                wx.hideLoading();
                let data = that.process(conversions);
                let noData = false;
                data.length == 0 ? noData = true : null;

                that.setData({ list: data, swith_ing: false, noData: noData });
            }, 1000);
        }
       

        
    },
    /**  生命周期函数--监听页面加载 */
    onLoad: function (options) {
        this.init();
    },

    /** 提醒发货 */
    remind:function(e){
        wx.showLoading({ title: '提醒发货' });
        // Api.fetch(, obj, resp => {
            const that = this;
            setTimeout(_ => {
                wx.hideLoading();
                wx.showToast({ title: '提醒成功' });
            }, 600)
        // })
    },

    navigateTo:function(e){
        const { tip, idx } = e.target.dataset;
        const { list } = this.data;
        let url, order_sn;
        idx ? order_sn = list[idx].order_sn:null;

        switch (tip){
            case 'logistics':
                url = `../logistics/logistics?order_sn=${order_sn}`
            break;
            case 'lottery':
                url = `../../points_goods_details/points_goods_details?order_sn=${order_sn}`
                break;
            case 'evaluation':
                url = `../evaluation/evaluation?order_sn=${order_sn}`
                break;
            case 'goMall':
                url = `../../points_mall/points_mall`
                break;
        };
        wx.navigateTo({ url: url });
    },
    check_details:function(){
        wx.navigateTo({ url: `../../points_goods_details/points_goods_details?gc_id=${100092}` });
    },
    /** ===== 前往完善信息 info ===== */

    fillInfo: function (e) {
        const that = this;
        wx.chooseAddress({
            success: function (res) {
                let address = res.provinceName + res.cityName + res.countyName + ',' + res.detailInfo;
                let userInfo = {
                    name: res.userName,
                    postalCode: res.postalCode,
                    address: address,
                    tel: res.telNumber
                };
                that.setData({
                    userInfo: userInfo
                })
            }
        })
    },
    /** dialog 显示与隐藏 */
    dialogShow: function () {
        let status;
        const { dialog } = this.data;
        dialog ? status = false : status = true;

        this.setData({ dialog: status });
    },
    /**  立刻兑换  */
    Expiry: function () {
              //哪一条数据    抽奖商品的id
        let { lottery_idx, lottery_id, list} = this.data;
        wx.showLoading({ title: '兑换中' });

        // Api.fetch(, obj, resp => {
            const that = this;
            setTimeout(_ => {
                wx.hideLoading();
                wx.showToast({ title: '兑换成功' });
                // Api.fetch(, obj, resp => { 
                    list[lottery_idx].status='1';  // 状态改为待发货
                    let data = that.process(list); // 修改后数据需要进行处理一下
                    that.setData({ list: data})
                // })
                that.dialogShow();
            }, 600)
        // })
    },

    /** ===== 前往完善信息 ===== */

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () { },

    /** 生命周期函数--监听页面显示 */
    onShow: function () {},

    /** 页面上拉触底事件的处理函数*/
    onReachBottom: function (e) {
        const that = this;
        const { isLoading, hasMore } = this.data;
        // 1 ==> 判断是否还有数据
        if (hasMore) {
            // 2 ==> 防止重复请求
            if (!isLoading) {
                this.setData({ isLoading: true });
                // 3 ==> 模拟请求
                setTimeout(_ => {
                    // 4 ==> 检测是否还有更多数据 hasMore
                    that.setData({ isLoading: false});
                    console.log('触底');
                }, 4000)
            }

        }
    },
    /** 用户点击右上角分享 */
    onShareAppMessage: function () {
    
    }
})